package com.nuanshui.heatedloan.credit.pay.behavior;

import com.aipg.common.AipgReq;
import com.aipg.common.AipgRsp;
import com.alibaba.fastjson.JSON;
import com.allinpay.XmlTools;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.credit.pay.*;
import com.nuanshui.heatedloan.credit.pay.annotation.PayBehaviorAttribute;
import com.nuanshui.heatedloan.credit.pay.constant.OrderPayState;
import com.nuanshui.heatedloan.credit.pay.constant.PayBehaviorType;
import com.nuanshui.heatedloan.credit.pay.utils.AllinPayUtil;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResult;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 开始发送报文数据
 */
@PayBehaviorAttribute(value= PayBehaviorType.PayExecuteBehavior,order=301)
public class PayExecuteBehavior extends PayBehavior {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderPayStateService orderPayStateService;

    @Autowired
    private AllinPayTool allinPayTool;

    @Override
    public void internalInvoke(PayRequest request) {

        AllinPayUtil.logOrderPay(logger, request, "通联支付通信发起开始");

        orderPayStateService.markState(request,OrderPayState.STATE_BEGIN);

        AipgReq aipgReq= PayContext.getContext().getAipgReq();

        aipgReq.getINFO().setTRX_CODE(request.getTransRequestCode());

        aipgReq.addTrx(request.getTrxData());

        orderPayStateService.markState(request,OrderPayState.STATE_LOCAL_PROCESSING);
        allinPayTool.sendReq(aipgReq);

        AipgRsp rsp=PayContext.getContext().getAipgRsp();

        AllinPayUtil.logOrderPay(logger, request, "通联支付通信发起结束: 请求数据："+ JSON.toJSONString(aipgReq));
        AllinPayUtil.logOrderPay(logger, request, "通联支付通信发起结束: 返回数据："+ JSON.toJSONString(rsp));
    }

    @Override
    public void internalValidate(ValidationResult result,PayRequest request) {
        super.internalValidate(result, request);
    }
}
